package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/29
 * @description ：
 * @version: 1.0
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Transactional
    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> monthList) {
        List<Integer> integerList = new ArrayList<>();
        for (int i = 0; i != monthList.size(); i++) {
            integerList.add(memberDao.findMemberCountBeforeDate(monthList.get(i) + ".31"));
        }
        return integerList;
    }

    @Override
    public List<Map<String, Object>> findSexCount() {
        return memberDao.findSexCount();
    }

    @Override
    public List<Map<String, Object>> findByBirthday() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar calendar=Calendar.getInstance();
            //18岁以内的
            calendar.add(Calendar.YEAR,-18);
            String oneDate = sdf.format(calendar.getTime());
            Integer shiba = memberDao.findByBirthday(oneDate);
            Map<String,Object> map=new HashMap<>();
            map.put("value",shiba);
            map.put("name","0-18岁");
            List<Map<String, Object>> list=new ArrayList<>();
            list.add(map);
            //18到30岁的
            calendar.add(Calendar.YEAR,-12);
            String twoDate = sdf.format(calendar.getTime());
            Integer beforesanshi = memberDao.findByBirthday(twoDate);
            Integer sanshi=beforesanshi-shiba;
            Map<String,Object> map1=new HashMap<>();

            map1.put("value",sanshi);
            map1.put("name","18-30岁");
            list.add(map1);
            //30到45岁的
            calendar.add(Calendar.YEAR,-15);
            String threeDate=sdf.format(calendar.getTime());
            Integer s = memberDao.findByBirthday(threeDate);
            Integer sishiwu=s-beforesanshi;
            Map<String,Object> map2=new HashMap<>();

            map2.put("value",sishiwu);
            map2.put("name","30-45岁");
            list.add(map2);


            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }
}
